package com.ruoyi.web.controller.controllers;


import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.provider.domain.AhctCase;
import com.ruoyi.provider.domain.AhctCaseCode;
import com.ruoyi.provider.service.AhctDefineDamageService;
import com.ruoyi.provider.service.AhctInformationService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/*
 * 案件信息管理
 */

@RestController
@RequestMapping("/case_management")
public class AhctCaseController extends BaseController {


    @Resource
    private AhctInformationService isSysInformationService;

    @Resource
    private AhctDefineDamageService ahctDefineDamageService;



    //获取案件管理列表
    @GetMapping("/getCaseCondition")
    public AjaxResult getCaseCondition(AhctCase ahctCase, int pageNum, int pageSize){

        try {
            PageInfo<AhctCase> pageInfo = isSysInformationService.getCaseCondition(ahctCase,pageNum,pageSize);
            return success(pageInfo);
        }catch (Exception e) {
            throw new ServiceException("参数异常错误", HttpStatus.BAD_REQUEST);
        }
    }

    //案件管理信息录入
    @Transactional
    @PostMapping("/insertCase")
    public AjaxResult insertCase(@RequestBody AhctCaseCode ahctCaseCode){

        //新增案件信息
        isSysInformationService.insertCase(ahctCaseCode);

        //新增案件信息同时新增到定损页面
        ahctDefineDamageService.insertMeanwhile(ahctCaseCode);

        return success(0);
    }

    //案件管理信息修改
    @PostMapping("/updateCase")
    public AjaxResult updateCase(@RequestBody AhctCaseCode ahctCaseCode){

        isSysInformationService.updateCase(ahctCaseCode);
        return success(0);
    }

    //案件管理信息删除
    @PostMapping("/deleteCase")
    public AjaxResult deleteCase(@RequestBody AhctCase ahctCase){

        isSysInformationService.deleteCase(ahctCase);
        return success(0);
    }


    //批量导入
    @Log(title = "案件信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importDataCase")
    public AjaxResult importDataCase(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<AhctCase> util = new ExcelUtil<AhctCase>(AhctCase.class);
        List<AhctCase> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = isSysInformationService.importDataCase(userList, updateSupport, operName);
        return success(message);
    }

    //批量导出
    @Log(title = "案件信息", businessType = BusinessType.EXPORT)
    @PostMapping("/exportCase")
    public void export(HttpServletResponse response, AhctCase ahctCase)
    {
        List<AhctCase> list = isSysInformationService.selectCaseList(ahctCase);
        ExcelUtil<AhctCase> util = new ExcelUtil<AhctCase>(AhctCase.class);
        util.exportExcel(response, list, "案件登记");
    }

    //下载模版
    @PostMapping("/importTemplateCase")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<AhctCase> util = new ExcelUtil<AhctCase>(AhctCase.class);
        util.importTemplateExcel(response, "用户数据");
    }
}
